import {
    generateRandomId,
    StrategyKey,
    generateClassToId,
    getDomList,
    getDom,
    addErrorClass
} from '@/utils';
import type { BaseTableConfig } from '@/components/base-ui/base-table/type';
import type { StrategyParams } from '@/utils';
import type {
    WorkingProcedureEquipments,
    WorkingProcedureAuxiliary
} from '@/api/procedure/model/type';

/**
 * 设备编码&辅助资源编码校验
 */
function getRuleFn(key: string, id: string) {
    let auxiliaryData: WorkingProcedureAuxiliary;

    return (params: StrategyParams) => {
        if (!params.value) {
            const tableBodyEl = getDom('.el-table__body', params.tableInstance.$el);
            const tableRowEl = getDomList('.el-table__row', tableBodyEl!)?.[params.row! - 1];

            let validErrorData = null;

            if (id === '1') {
                // 获取列表中设备编码为空的数据
                validErrorData = params.tableInstance.data.filter(
                    (item: WorkingProcedureEquipments) => !item[key]
                )[0];
            } else {
                // 获取列表中辅助资源编码为空的数据
                auxiliaryData =
                    params.tableInstance.data[params.row! - 1]?.workingProcedureAuxiliary;
                if (auxiliaryData) {
                    validErrorData = auxiliaryData.filter(
                        (item: WorkingProcedureAuxiliary) => !item[key]
                    )[0];
                }
            }

            if (validErrorData) {
                let validErrorEl: HTMLElement | undefined;
                if (id === '1') {
                    // 获取设备编码校验失败 dom 元素
                    validErrorEl = getDomList(
                        `.${generateClassToId(id)}`,
                        tableRowEl as HTMLElement
                    )?.[0];
                } else {
                    // 获取辅助资源编码校验失败 dom 元素
                    const errIndex = auxiliaryData.findIndex(
                        (item: WorkingProcedureAuxiliary) => !item[key]
                    );

                    validErrorEl = getDomList(
                        `.${generateClassToId(id)}`,
                        tableRowEl as HTMLElement
                    )?.[errIndex];
                }

                addErrorClass(validErrorEl!);
                return '请完善必填信息';
            }
        }
    };
}

export const tableConfig: BaseTableConfig = {
    items: [
        {
            modelKey: 'equipmentNumber',
            slotName: 'workingProcedureEquipments',
            noEdit: true,
            columnProps: {
                label: '设备编码',
                width: 220,
                cellClassName: 'custom-cell',
                customRequired: true
            },
            validProps: {
                id: generateRandomId()
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('equipmentNumber', '1'),
                    errMsgFn: () => {
                        return `请完善设备编码信息`;
                    }
                }
            ]
        },
        {
            modelKey: 'deviceName',
            slotName: 'workingProcedureEquipmentsName',
            noEdit: true,
            columnProps: {
                label: '设备名称',
                width: 200,
                cellClassName: 'custom-cell',
                customRequired: true
            },
            validProps: {
                id: generateRandomId()
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: (params: any) => {
                        if (!params.value) {
                            return '请完善必填信息';
                        }
                    },
                    errMsgFn: () => {
                        return `请重新选择设备编码并完善设备名称`;
                    }
                }
            ]
        },
        {
            modelKey: 'auxiliaryDataCoding',
            // 自定义校验值定义
            validValueFn: (data: WorkingProcedureEquipments, key: string) => {
                return !data.workingProcedureAuxiliary.some(item => !item[key]);
            },
            slotName: 'workingProcedureAuxiliary',
            noEdit: true,
            columnProps: {
                label: '辅助资源编码',
                width: 220,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId()
            }
            // validRules: [
            //     {
            //         validType: StrategyKey.IS_CUSTOMIZE,
            //         ruleFn: getRuleFn('auxiliaryDataCoding', '2'),
            //         errMsgFn: () => {
            //             return `请完善辅助资源编码信息`;
            //         }
            //     }
            // ]
        },
        {
            modelKey: 'auxiliaryDataCoding',
            slotName: 'workingProcedureAuxiliaryName',
            noEdit: true,
            columnProps: {
                label: '辅助资源名称',
                width: 200,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId()
            }
        }
    ],
    unAction: true,
    unIndex: true,
    unSelection: true
};
